import { isWechat } from '@/config/h5Utils';
import { setCookie,getCookie,removeCookie } from '@/plugins/cookie';
import { getUserInfo,getWxTokenByUnionId } from  '@/config/api.js'
import { inquiryCartInfo } from  '@/config/points.js'
export const state = {
    //用户数据
    userInfo: {},
	quotationNum:0,
	tablist: [{
	    name: '首页',
	    url: 'pages/index/index',
	    icon: '/static/index.png',
	    iconFill: '/static/index-s.png'
	  },
	  {
	    name: '询价',
	    url: 'pages/inquiry/inquiryList',
	    icon: '/static/list.png',
	    iconFill: '/static/list-s.png',
	    badge: 0
	  },
	  {
	    name: '发布询价',
	    url: 'pages/public/addInquiry',
	    icon: '/static/add.png',
	    iconFill: '/static/add-s.png',
		dot: false
	  },
	  {
	    name: '行情',
	    url: 'pages/public/price',
	    icon: '/static/price.png',
	    iconFill: '/static/price-s.png',
		dot: false
	  },
	  {
	    name: '我的',
	    url: 'pages/my/index',
	    icon: '/static/my.png',
	    iconFill: '/static/my-s.png',
	    dot: false
	  }
	],
	carInfo:0
};
export const mutations = {
	//储存报价中数量
	setQuotationNum(state, data) {
			uni.$set(state,'quotationNum',data) 
			 // console.log("state.state.user.quotationNum",state.quotationNum)  
			 let list = JSON.parse(JSON.stringify(state.tablist))
			 let c = JSON.parse(JSON.stringify(list[1]))
			 c.badge = data;
			 list.splice(1,1,c)
			 uni.$set(state,'tablist',list) 
	},
    //储存用户信息
    setUserInfo(state, data) {
		if (data) {
			 state.userInfo =  Object.assign({}, state.userInfo,data);
			//  console.log("state.userInfo",state.userInfo)
			 // #ifdef H5
			 // if(isWechat()){
				// 微信浏览器使用cookie存储---ios微信浏览器使用此方式可以拿到cookie
				var val = encodeURIComponent(JSON.stringify(state.userInfo)) //这里对保存的数据进行编码
				setCookie('userInfo',val,30)
			 // }else{
			 // 	window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo));
			 // }
			 // #endif
			 // #ifndef H5
			 uni.setStorageSync('userInfo', state.userInfo);
			 // #endif
		}
        
    },
    // 退出APP
    emptyUserInfo(state) {
        state.userInfo = {};
        // #ifdef H5
        // if(isWechat()){
        	removeCookie('userInfo')
        // }else{
        // 	window.sessionStorage.removeItem("userInfo");
        // }
        // #endif
        // #ifndef H5
        uni.removeStorageSync("userInfo");
        // #endif
		
		if(uni.getStorageSync('qdClosed')){
			uni.removeStorageSync('qdClosed');
		}
    },
	
	//存储购物车数量
	setCarInfo(state, data){
		console.log("data",data)
		state.carInfo =  data;
		console.log("state.carInfo",state.carInfo)
	},

};
export const actions = {
   userInfo(context,unionid){
	   let fn = unionid ? getWxTokenByUnionId : getUserInfo
	   let data = {}
	   if(unionid){
		   data.unionid = unionid
	   }
	  return fn(data).then((res)=>{
	   		let storeUserInfo = {}
	   		storeUserInfo.mobile = res.userInfo && res.userInfo.mobile ? res.userInfo.mobile : '';
	   		storeUserInfo.userHeader = res.userInfo && res.userInfo.userHeader ? res.userInfo.userHeader : ''
	   		storeUserInfo.userName = res.userInfo && res.userInfo.userName ? res.userInfo.userName : '';
	   		storeUserInfo.realName  = res.userInfo && res.userInfo.realName ? res.userInfo.realName : '';
	   		storeUserInfo.userId = res.userInfo && res.userInfo.userId ? res.userInfo.userId : '';
	   		// storeUserInfo.wxOpenId = res.userInfo && res.userInfo.wxOpenId ? res.userInfo.wxOpenId : '';
	   		// storeUserInfo.wxUnionId = res.userInfo && res.userInfo.wxUnionId ? res.userInfo.wxUnionId : '';
	   		storeUserInfo.promoterCode = res.userInfo && res.userInfo.promoterCode ? res.userInfo.promoterCode : '';
	   		storeUserInfo.isBuyerAuth = res.userInfo && res.userInfo.isBuyerAuth ? res.userInfo.isBuyerAuth : 0;
	   		storeUserInfo.isSellerAuth = res.userInfo && res.userInfo.isSellerAuth ? res.userInfo.isSellerAuth : 0;
	   		storeUserInfo.storeId = res.userInfo && res.userInfo.storeId ? res.userInfo.storeId : '';
			storeUserInfo.storeName = res.userInfo && res.userInfo.storeName ? res.userInfo.storeName : '';
	   		storeUserInfo.permissions = res.userInfo && res.userInfo.permissions ? res.userInfo.permissions : [];
	   		storeUserInfo.newUserMsg   = res.userInfo && res.userInfo.newUserMsg   ? res.userInfo.newUserMsg   : '';
	   		storeUserInfo.noAuthMsg = res.userInfo && res.userInfo.noAuthMsg ? res.userInfo.noAuthMsg : '';
			storeUserInfo.balance = res.userInfo && res.userInfo.balance ? res.userInfo.balance : 0;
			storeUserInfo.token = res.token;
	   		context.commit('setUserInfo',storeUserInfo)
	   })
   },
   
   getInquiryCartInfo(context){
   	  return inquiryCartInfo().then((res)=>{
   	   		context.commit('setCarInfo',res.cartGoodsNum)
   	   })
   }
   		
   	
  
};
